import 'package:cher_flare/http/http_utils.dart';
import 'package:cher_flare/modules/login/models/ly_login_info_manager.dart';
import 'package:cher_flare/routes/app_pages.dart';
import 'package:cher_flare/utils/ly_data_manager.dart';
import 'package:cher_flare/utils/ly_medal_manager.dart';
import 'package:cher_flare/utils/ly_use_log_manager.dart';
import 'package:cher_flare/utils/size_fit.dart';
import 'package:cher_flare/utils/utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../config/config.dart';
import '../../config/url_api.dart';
import '../../utils/app_color.dart';
import '../../utils/ly_medal_manager.dart';

class LYSettingPage extends StatefulWidget {
  const LYSettingPage({super.key});

  @override
  State<LYSettingPage> createState() => _LYSettingPageState();
}

class _LYSettingPageState extends State<LYSettingPage> {
  void _translateSwitchDidChanged(bool value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool('autoTranslate', value);
  }

  void _logoutDidClick() async {
    LYUseLogManager.stat(page: LYLogPages.quickLogin);
    EasyLoading.show();
    final response = await HttpUtils.getInstance.post(URLApi.logout);
    if (response.isSuccess) {
      LYLoginInfoManager.getInstance.logout();
      Get.offAllNamed(AppPages.login);
    }
    EasyLoading.dismiss();
  }

  void _deleteAccountDidClick() {
    LYUseLogManager.stat(page: LYLogPages.deleteAccount);
    Get.dialog(CupertinoAlertDialog(
      title: const Text('Delete account'),
      actions: [
        TextButton(
            onPressed: () {
              Get.close(0);
            },
            child: const Text('cancel')),
        TextButton(
            onPressed: () async {
              Get.close(0);
              EasyLoading.show();
              final response =
                  await HttpUtils.getInstance.post(URLApi.deleteAccount);
              LYDataManager.share.removeAllData();
              if (response.isSuccess) {
                await LYHomeDataManager.share.clearData();
                LYLoginInfoManager.getInstance.logout();
                Get.offAllNamed(AppPages.login);
              }
              EasyLoading.dismiss();
            },
            child: const Text('sure')),
      ],
    ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Utils.commonAppBar(title: 'Setting'),
      backgroundColor: AppColors.whiteColor,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          const SizedBox(
            height: 8,
          ),
          Container(
            height: 48.px,
            alignment: Alignment.centerLeft,
            margin: const EdgeInsets.only(left: 16, right: 16),
            padding: const EdgeInsets.symmetric(horizontal: 12),
            decoration: BoxDecoration(
              color: AppColors.colorF6F8FA,
              borderRadius: BorderRadius.circular(8),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  'ID:${LYLoginInfoManager.getInstance.loginModel?.userInfo?.userId ?? ''}',
                  style: TextStyle(fontSize: 16, color: AppColors.color404040),
                ),
                InkWell(
                  onTap: () {
                    Clipboard.setData(ClipboardData(
                        text: LYLoginInfoManager
                            .getInstance.loginModel?.userInfo?.userId));
                    EasyLoading.showToast('Copied');
                  },
                  child: Container(
                    alignment: Alignment.center,
                    padding: EdgeInsets.symmetric(horizontal: 6.px),
                    height: 20.px,
                    decoration: BoxDecoration(
                      boxShadow: [
                        BoxShadow(
                            color: colorWithRGBA(r: 0, g: 0, b: 0, a: 0.08),
                            blurRadius: 20.0)
                      ],
                      color: Colors.white,
                      borderRadius: BorderRadius.circular((10.px)),
                    ),
                    child: Text(
                      'copy',
                      style: TextStyle(
                          color: AppColors.color44FC04, fontSize: 10.px),
                    ),
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(
            height: 16,
          ),
          Container(
            height: 48.px,
            alignment: Alignment.centerLeft,
            margin: const EdgeInsets.only(left: 16, right: 16),
            padding: const EdgeInsets.symmetric(horizontal: 12),
            decoration: BoxDecoration(
              color: AppColors.colorF6F8FA,
              borderRadius: BorderRadius.circular(8),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                const Text(
                  'Auto Translate',
                  style: TextStyle(fontSize: 16, color: AppColors.color404040),
                ),
                CupertinoSwitch(
                    value: Config.autoTranslate,
                    onChanged: (value) {
                      Config.autoTranslate = value;
                      setState(() {});
                      _translateSwitchDidChanged(value);
                    },
                    activeColor: AppColors.color86FB8C),
              ],
            ),
          ),
          const SizedBox(
            height: 16,
          ),
          InkWell(
            onTap: () => _logoutDidClick(),
            child: Container(
              height: 48.px,
              alignment: Alignment.centerLeft,
              margin: const EdgeInsets.only(left: 16, right: 16),
              padding: const EdgeInsets.symmetric(horizontal: 12),
              decoration: BoxDecoration(
                color: AppColors.colorF6F8FA,
                borderRadius: BorderRadius.circular(8),
              ),
              child: const Text(
                'Logout',
                style: TextStyle(fontSize: 16, color: AppColors.color404040),
              ),
            ),
          ),
          const SizedBox(
            height: 16,
          ),
          InkWell(
            onTap: () => _deleteAccountDidClick(),
            child: Container(
              height: 48.px,
              alignment: Alignment.centerLeft,
              margin: const EdgeInsets.only(left: 16, right: 16),
              padding: const EdgeInsets.symmetric(horizontal: 12),
              decoration: BoxDecoration(
                color: AppColors.colorF6F8FA,
                borderRadius: BorderRadius.circular(8),
              ),
              child: const Text(
                'Delete account',
                style: TextStyle(fontSize: 16, color: AppColors.color404040),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
